package com.nice.nice201110.controller;

import com.nice.nice201110.entity.TBaseDisease;
import com.nice.nice201110.service.TbaseDiseaseService;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RestController
@RequestMapping("/admin/tbasedisease")
public class TbaseDiseaseController {

    @Autowired
    TbaseDiseaseService tbaseDiseaseService;

    private Map<String,Object> codeMes = new HashMap<>();

    /**
     *  查看疾病列表
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/list")
    public Map<String,Object> getlist(HttpServletRequest request, @RequestBody(required = false) BaseVo baseVo){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code", NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        if(baseVo!=null){
            baseVo.setUservo(userVo);
        }else{
            baseVo = new BaseVo();
            baseVo.setUservo(userVo);
        }
        List<TBaseDisease> tBaseDiseases = tbaseDiseaseService.getlist(baseVo);

        codeMes.put("code", NiceEnum.code_200);
        codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        codeMes.put("data", tBaseDiseases);
        return codeMes;
    }
    /**
     *   查询单一疾病信息
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/one")
    public Map<String,Object> getOne(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id) {
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        TBaseDisease tBaseDisease = tbaseDiseaseService.getOne(userVo,id);
        if(tBaseDisease!=null){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseDisease);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**更新疾病基本信息*/
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT,value="/update")
    public Map<String,Object> updateTbaseDisease(HttpServletRequest request,@RequestBody TBaseDisease tBaseDisease){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
       Boolean blean = tbaseDiseaseService.updateTbaseDisease(userVo,tBaseDisease);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     *  插入数据
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/insert")
    public Map<String,Object> insetTbaseDisease(HttpServletRequest request, @RequestBody(required = false) TBaseDisease tBaseDisease){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes = new HashMap<>();
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseDiseaseService.insertTbaseDisease(userVo,tBaseDisease);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     *  删除数据
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value="/delete")
    public Map<String,Object> deleteTbaseDisease(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseDiseaseService.deletetBaseDisease(userVo,id);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     * 删除数据s
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value="/deletes")
    public Map<String,Object> deletesTbaseCapital(HttpServletRequest request,@RequestParam(name = "ids",required=true) String ids){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseDiseaseService.deletetsBaseDisease(userVo,ids);
        if (blean) {
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;

        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
}
